import {UnitModel} from "../../frame/apiModel/adapterModel/model3/UnitModel";
import {Logger} from "../../frame/apiModel/config/Logger";
import {PlayerFactory} from "../../frame/PlayerFactory";
import {UnitController} from "../../controller/UnitController";
import {DataRouterUtil} from "../../utils/router/DataRouterUtil";
import {CodeUtil} from "../../frame/codeAdapter/CodeUtil";
import {AttributeEnum} from "../../frame/dataSource/DataModel";
import {Util} from "../../utils/Util";
import {PlayerModel} from "../../frame/apiModel/adapterModel/model4/PlayerModel";
import {PlayerDataKey} from "../../frame/dataSource/DataSource";
import {AbilityController} from "../../controller/AbilityController";
import UnitUtil from "../../utils/UnitUtil";
import {GuiControllerOpt, GuiId, UISkillObj} from "../../controller/model/ControllerOpt";
import {GuiController, OptType} from "../../controller/GuiController";
import {ItemModel} from "../../frame/apiModel/adapterModel/model3/ItemModel";
import {DataRouter} from "../../frame/dataSource/DataRouter";
import {ItemJson} from "../../frame/apiModel/model/StoreJson";
import {ModelFactory} from "../../frame/apiModel/adapterModel/model5/ModelFactory";
import {GoodController} from "../../controller/GoodController";
import Configer from "../../frame/apiModel/config/Configer";
import {UIConfigKey} from "../../constant/UIConfig";

export class AbilityUtil {
    static catchPet (activeUnit: UnitModel, targetUnit: UnitModel, index: number, probability: number = 0, exp: number = 0) {
        Logger.toastSys('开始捕捉！！！');
        const targetPlayer = PlayerFactory.getInstance().getUnitOwner(targetUnit);
        const activePlayer = PlayerFactory.getInstance().getUnitOwner(activeUnit);
        const isPlayer = targetPlayer?.isPlaying();
        const isPlayerAlly = activePlayer.isPlayerAlly(targetPlayer);
        if (isPlayerAlly && isPlayer) {
            Logger.toastProduct('不要调戏队友的宠物！', activePlayer);
            return false;
        }

        if (isPlayerAlly) {
            UnitController.getInstance().toEnemyTeam(targetUnit);
        }

        const unitId = targetUnit.strId;
        const isPet = DataRouterUtil.checkIsPet(targetUnit);
        if (isPet) {
        } else {
            Logger.toastProduct('该生物不能捕捉！', activePlayer);
            return false;
        }
        const petData = DataRouterUtil.getPetDataByCatchId(unitId);
        if (petData) {
        } else {
            Logger.toastProduct('该生物不能捕捉，因为它没有进行petData设置', activePlayer);
            return false;
        }

        const targetLv = targetUnit.level;
        const activeLv = activeUnit.level;

        if (targetLv > activeLv) {
            Logger.toastProduct('不能捕捉比自己等级高的宠物！', activePlayer);
            return false;
        }

        const percentage = targetUnit.lifePercent;
        const petLifePercentageStr = petData.catch_life;
        const petRadixStr = petData.catch_probability;
        const petLifePercentageArr = CodeUtil.getArrBySplit(petLifePercentageStr);
        const petRadixArr = CodeUtil.getArrBySplit(petRadixStr);

        const catch_compensate_radix = petData.catch_compensate ? petData.catch_compensate : 0;

        Logger.toastSys(`怪物当前血量比: ${percentage}`, activePlayer);

        const newstAttr = UnitController.getInstance().getPlayerAttr(activePlayer);
        const catchRadix = (newstAttr && newstAttr[AttributeEnum.catchRadix]) ? newstAttr[AttributeEnum.catchRadix] : 0;
        const catchCompensate = UnitController.getInstance().getPlayerUnitCatchCompensate(activePlayer, targetUnit);

        let success = false, bCatch = false;
        for (let i = 0; i < petLifePercentageArr.length; i++) {
            const petLifePercentage = DataRouterUtil.getNumByArr(petLifePercentageArr, i);
            const numPercentage = petLifePercentage / 100;
            Logger.toastSys(`捕捉所需生命比例: ${numPercentage}`, activePlayer);
            if (percentage < numPercentage) {
                bCatch = true;
                const radix = DataRouterUtil.getNumByArr(petRadixArr, i);
                const numRadix = radix + catchRadix + catchCompensate;
                // if (probability === 0) {
                //     Logger.toastProduct('当前捕捉几率: 100', activePlayer);
                // } else {
                //     Logger.toastProduct(`当前捕捉几率: ${numRadix}（含捕捉概率增强+${catchRadix}, 概率补偿+${catchCompensate}）`, activePlayer);
                // }
                if (numRadix > probability) {
                    success = true
                }
                break;
            } else {
                continue;
            }
        }
        if (bCatch) {
            if (success) {
                UnitController.getInstance().catchPet(activeUnit, targetUnit, activePlayer, index, exp);
                UnitController.getInstance().setPlayerUnitCatchCompensate(activePlayer, targetUnit, undefined);
                return true;
            } else {
                const newCatchCompensate = catchCompensate + catch_compensate_radix;
                UnitController.getInstance().setPlayerUnitCatchCompensate(activePlayer, targetUnit, newCatchCompensate);
                Logger.toastProduct(`捕捉失败，该宠物捕捉概率增加 ${catch_compensate_radix}%`, activePlayer);
            }
        } else {
            Logger.toastProduct(`生命值过高，无法捕捉`, activePlayer);
        }
        return false
    }
    // static reflushSelectedUI(selectedPlayer: PlayerModel) {
    //     const unit = selectedPlayer.data[PlayerDataKey.selectedUnit]
    //     if (unit) {
    //         const buffs = AbilityController.getInstance().getBuffs(unit);
    //         const attr = UnitUtil.getAttribute(unit);
    //         const newestAttr = attr.newestAttribute;
    //         const request = { buffs, attr: newestAttr, unit: unit };
    //         const opt = new GuiControllerOpt(GuiId.buffBar, request);
    //         GuiController.getInstance().showGui(opt, selectedPlayer, OptType.reflush);
    //     }
    // }
    static eatAUnit(activeUnit: UnitModel, num: number): string {
        const activeUnitId = activeUnit.strId;

        const player = PlayerFactory.getInstance().getUnitOwner(activeUnit);
        const nextUnitId = DataRouterUtil.getNextUnitIdByFamilyId(activeUnitId, num);
        if (nextUnitId) {
            return nextUnitId;
        } else {
            Logger.toastProduct(`单位已经满级，无法继续提升`, player)
        }
        return;
        // const currentLevel = GetHeroLevel(activeUnit);

        // const maxEatLevel = 5;
        // if (currentLevel >= maxEatLevel) {
        //     Util.loggerInDev(`单位已经满级，无法继续提升`, LogType.product, ToastType.text, player)
        //     return false;
        // }
        // let newLevel = currentLevel + levelAdd;
        //
        // if (newLevel >= maxEatLevel) {
        //     newLevel = maxEatLevel;
        // }
        // SetHeroLevel(activeUnit, newLevel, true);
        //
        // const unitData = DataRouter.getUnitDataByUnitId(activeUnitId);
        // if (unitData) {
        //     // const talent = unitData.talent;
        //     // const learnTimes = newLevel - currentLevel;
        //     // for (let i = 0; i < learnTimes; i++) {
        //     //     const familyId = DataRouter.getFamilyIdByAbilityId(talent);
        //     //     AbilityController.getInstance().learnAbilityByFamilyIdCheck(activeUnit, familyId)
        //     // }
        // }
    }

    static useUISkill(player: PlayerModel, skillId: string) {
        const uiSkillMap: UISkillObj[] = Configer.getUIConfig(UIConfigKey.uiSkillMap);
        for (let i = 0; i < uiSkillMap.length; i++) {
            const obj = uiSkillMap[i];
            const tempSkillId = obj.skillId;
            if (tempSkillId === skillId) {
                const guiId = obj.guiId;
                const opt = new GuiControllerOpt(guiId, { });
                GuiController.getInstance().showGui(opt, player, OptType.reverse);
                return;
            }
        }
    }
}